function formatNumber(n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime(date, type) {
  // console.log(date)
  var date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  if (type == 'data') {
    const t1 = [year, month, day].map(formatNumber).join('.')
    return `${t1}`
  } else {
    const t1 = [year, month, day].map(formatNumber).join('-')
    const t2 = [hour, minute, second].map(formatNumber).join(':')
    return `${t1} ${t2}`
  }


}
export function fadeUp() {
  const animation = wx.createAnimation({
    duration: 0
  });
  animation.scale(0).step();
  return animation.export();
}
export function fadeIn() {
  const animation = wx.createAnimation({
    duration: 500
  });
  animation.scale(1).step();
  return animation.export();
}
export function Toast(title) {
  wx.showToast({
    title,
    icon: 'none'
  })
}
export function formatDuring(mss) {
  let str = ''
  var days = parseInt(mss / (1000 * 60 * 60 * 24));
  var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = (mss % (1000 * 60)) / 1000;
  if (days > 0) {
    str = days.toString() + '天前'
  } else if (hours > 0) {
    str = parseInt(hours) + '小时前'
  } else if (minutes > 0) {
    str = parseInt(minutes) + '分钟前'
  } else if (seconds > 0) {
    str = '刚刚'
  } else if (seconds < 0) {
    str = '刚刚'

  }
  return str
}
// 下拉刷新
export async function onfetch(api, size, page) {
  wx.showNavigationBarLoading();
  const res = await api({
    size,
    page
  })
  if(res.data.code==200){
    wx.hideNavigationBarLoading();
    res.data.data.content.map(item=>{
      item.createTime = formatTime(item.createTime);
    })

    return res.data.data.content
  }
}
// 上拉加载
export async function onbottom(api, size, page,totalpage,orderStatus) {
  if(!orderStatus){
    console.log(page,totalpage)
    if(page>totalpage){
      Toast('没有更多数据了');
      return
    }
    const res = await api({
      size,
      page
    })
    if (res.data.code == 200) {
      res.data.data.content.map(item=>{
        item.createTime = formatTime(item.createTime);
      })
      return res.data.data.content
    }
  }else{
    console.log(page,totalpage)
    if(page>totalpage){
      Toast('没有更多数据了');
      return
    }
    const res = await api({
      size,
      page,
      orderStatus
    })
    if (res.data.code == 200) {
      res.data.data.content.map(item=>{
        item.createTime = formatTime(item.createTime);
      })
      return res.data.data.content
    }
  }

}
export default {
  formatNumber,
  formatTime,
  fadeUp,
  Toast,
  formatDuring,
  fadeIn,
  onfetch,
  onbottom
}
